import random

import pygame

from actor.mine_boom import Boom, Mine
from actor.shoot_bullet import Bullet
from dialog.final_dialog import FinalTask1StartDialog, FinalTaskFailDialog
from scene.task.stage_one import BaseTask, TaskStatus

"""
寻找武器任务类
"""
class FindWeapon(BaseTask):
    def __init__(self, xiao_tie, japanese, mines_group, font):
        """
        相应参数初始化
        :param xiao_tie: 小铁类
        :param japanese: 日军精灵组
        :param mines_group: 地雷精灵组
        :param font: 字体类
        """
        self.japanese = japanese
        self.xiao_tie = xiao_tie
        self.mines_group = mines_group
        self.mines = pygame.sprite.Group()
        self.booms = pygame.sprite.Group()
        self.bullets = pygame.sprite.Group()
        self.score = 0
        mine = pygame.image.load("./resources/images/mine/地雷.png")
        self.mine_image = pygame.transform.scale(mine, (40, 40))
        self.boom_image = pygame.image.load("./resources/images/mine/boom.png")
        self.font = font
        self.sound = pygame.mixer.Sound('./resources/sound/prizes.mp3')
        self.task_dialog = None
        self.win_dialog = None
        self.fail_dialog = None
        self.init_dialog()

        super(FindWeapon, self).__init__(self.task_dialog, self.win_dialog,
                                               self.fail_dialog)

    def init_dialog(self):
        """
        对话初始化
        :return:
        """
        self.task_dialog = FinalTask1StartDialog(self.font)
        self.win_dialog = None
        self.fail_dialog = FinalTaskFailDialog(self.font)

    def do_collide_group(self):
        """
        精灵之间的碰撞检测
        :return:
        """
        is_collide_xiao_tie_japanese = pygame.sprite.spritecollide(self.xiao_tie, self.japanese, False)
        is_collide_xiao_tie_mines_group = pygame.sprite.spritecollide(self.xiao_tie, self.mines_group, True)
        is_collide_xiao_tie_mines = pygame.sprite.spritecollide(self.xiao_tie, self.mines, True)
        if is_collide_xiao_tie_mines_group or is_collide_xiao_tie_mines:
            self.xiao_tie.boom_count += 1
            self.sound.play()
        if is_collide_xiao_tie_japanese:
            self.xiao_tie.hp -= 50
            if self.xiao_tie.hp <= 0:
                self.task_status = TaskStatus.fail_dialog
        if self.xiao_tie.boom_count == 2:
            self.xiao_tie.set_count(self.xiao_tie.boom_count + 2)
            self.task_status = TaskStatus.win_dialog

    def do_self_task(self, key_down, key_list):
        """
        具体任务执行
        :param key_down:
        :param key_list:
        :return:
        """
        self.do_collide_group()
        if key_down and key_list[pygame.K_SPACE]:
            if self.xiao_tie.boom_count > 0:
                self.create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)
                self.xiao_tie.boom_count -= 1

    def create_boom(self, x, y):
        """
        爆炸特效
        :param x:
        :param y:
        :return:
        """
        mine = Boom(x, y, self.boom_image, 4, 3)
        self.booms.add(mine)

    def create_mine(self, x, y):
        """
        埋地雷
        :param x:
        :param y:
        :return:
        """
        mine = Mine(x, y, self.mine_image)
        self.mines.add(mine)


    def draw_task(self, current_surface, x, y):
        """
        绘制任务对应角色
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        for mine in self.mines:
            mine.draw(current_surface, x, y)
        for boom in self.booms:
            boom.draw(current_surface, x, y)